package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.FootmarkDao;
import com.baichang.taoshunda.entity.Footmark;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class FootmarkDaoImpl extends BaseDaoImpl<Footmark> implements FootmarkDao {
    /**
     * 查询我的足迹
     *
     * @param userId   用户编号
     * @param nowPage  当前页
     * @param pageSize 当前条数
     * @param key
     * @return
     */
    @Override
    public List<Map<String, Object>> findFoorMark(Integer userId, Integer nowPage, Integer pageSize, String key) {
        String sql = "SELECT f.id, g.id AS goodsId, g.company_id AS busId,g.unit unit, g.head_pic headPic, g.price , " +
                "g.month_sales AS saleNum, g.city_id cityId, g.`name` AS goodsName, g.is_invoice isInvoice," +
                " g.is_no_reason_return isNoReasonReturn, g.is_bespeak isBespeak, g.is_free isFree,g.is_home_installation" +
                " isHomeInstallation ,g.commercial_activities commercialActivities, g.is_special isSpecial" +
                " FROM t_footmark f INNER JOIN t_goods g ON g.id = f.goods_id AND g.`status` = 1 and g.is_upper = 1 where f.user_id= ? ";
        if (!key.equals("")) {
            sql += " AND g.`name` LIKE '%" + key + "%'";
        }
        sql += " order by f.id desc";
        System.out.println(sql);
        return listForMapList(sql, nowPage, pageSize, userId);
    }

    @Override
    public int getCountByUser(int userId, Integer goodsId) {
        String sql = "SELECT count(0) FROM `t_footmark` WHERE `user_id` = ? AND `goods_id` = ?  ";
        return queryForInt(sql, userId, goodsId);
    }

    @Override
    public void updateTime(int userId, Integer goodsId) {
        String sql = "UPDATE t_footmark SET created=NOW()   WHERE `user_id` = ? AND `goods_id` = ? ";
        update(sql, userId, goodsId);
    }

    @Override
    public boolean deleteAllFootMark(Integer userId) {
        return delete(" user_id=?", userId) > 0;
    }

    @Override
    public boolean deleteSelectedFootMark(String footMarkId) {

        return delete("id in (" + footMarkId + ")") > 0;
    }
}